﻿/*
 * lcd.c
 *
 * Created: 2014-03-10 22:56:05
 *  Author: Michał
 */ 

#include "lcd.h"
#include <avr/io.h>


void LCD_Init(void)
{
	LCD_RS_DDR |= (1<<LCD_SI_PIN);
	LCD_SI_DDR |= (1<<LCD_SI_PIN);
	LCD_ENABLE_DDR |= (1<<LCD_ENABLE_PIN);
	CLK_DDR |= (1<<CLK_PIN);
	LCD_ENABLE_LOW;
	LCD_RS_LOW;
	LCD_SI_LOW;
	
	_delay_ms(20);
	LCD_Send_Commend(0x01);
	_delay_ms(10);
	LCD_Send_Commend(HD44780_FUNCTION_SET | HD44780_FONT5x7 | HD44780_TWO_LINE | HD44780_8_BIT); 
	LCD_Send_Commend(HD44780_DISPLAY_ONOFF | HD44780_DISPLAY_OFF); 
	LCD_Send_Commend(HD44780_CLEAR); 
	_delay_ms(2);
	LCD_Send_Commend(HD44780_ENTRY_MODE | HD44780_EM_SHIFT_CURSOR | HD44780_EM_INCREMENT);
	LCD_Send_Commend(HD44780_DISPLAY_ONOFF | HD44780_DISPLAY_ON | HD44780_CURSOR_OFF | HD44780_CURSOR_NOBLINK); 
}

void LCD_Send_Data(char data)
{
	uint8_t i=8;
	
	while(i--)
	{
		if(data&0x80) LCD_SI_HIGH;
		else LCD_SI_LOW;
		CLK_PULSE;
		data <<= 1;
	}
}

void LCD_Send_Commend(char data)
{
	LCD_Send_Data(data);
	LCD_RS_LOW;
	LCD_ENABLE_PULSE;
	_delay_us(50);
}



void LCD_Send_Char(char data)
{
	LCD_Send_Data(data);
	LCD_RS_HIGH;
	LCD_ENABLE_PULSE;
	_delay_us(50);
	LCD_RS_LOW;
}


void LCD_Text(char* text)
{
	while(*text) LCD_Send_Char(*text++);
}


void LCD_Clear(void)
{
	LCD_Send_Commend(HD44780_CLEAR);
	_delay_ms(2);
}


void LCD_Home(void)
{
	LCD_Send_Commend(HD44780_HOME);
	_delay_ms(2);
}


void LCD_Position_LN(uint8_t l, uint8_t n)
{
	LCD_Send_Commend(HD44780_DDRAM_SET | ((n-1) + (0x40 * (l-1))));
}